package com.youlu.campus.admin.university;

import com.youlu.campus.admin.auth.entity.SystemUser;
import com.youlu.campus.admin.auth.service.UserService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.admin.utils.IpUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.UniversityStatisticsQueryVO;
import com.youlu.campus.entity.data.UniversityStatistics;
import com.youlu.campus.service.data.UniversityStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

@RestController
public class UniversityStatisticsController {
    @Autowired
    private UniversityStatisticsService universityStatisticsService;
    @Autowired
    private UserService userService;

    @PostMapping("/university-statistics/export")
    public RestResult exportUniversityStatistics(@RequestBody UniversityStatisticsQueryVO req, HttpServletRequest request) {
        req.setUserId(AuthUtils.getUserId());
        SystemUser systemUser = userService.get(req.getUserId());
        if (Objects.nonNull(systemUser)) {
            req.setOptUserName(systemUser.getLoginName());
            req.setOptUserRealName(systemUser.getRealName());
        }
        String ip = IpUtils.getIpAddr(request);
        req.setIpAddress(ip);
        universityStatisticsService.export(req);
        return RestResultBuilder.builder().success().build();
    }

    @PostMapping("/university-statistics/list")
    public RestResult list(@RequestBody UniversityStatisticsQueryVO req) {
        req.setUserId(AuthUtils.getUserId());
        Page<UniversityStatistics> datas = universityStatisticsService.list(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @GetMapping("/university-statistics/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        UniversityStatistics universityStatistics = universityStatisticsService.findOne(id);
        if (universityStatistics == null) {
            return RestResultBuilder.builder().success().data(null).build();
        }
        return RestResultBuilder.builder().success().data(universityStatistics).build();
    }

    @PostMapping("/university-statistics/all-data")
    public RestResult allData(@RequestBody UniversityStatisticsQueryVO req) {
        universityStatisticsService.allData(req);
        return RestResultBuilder.builder().success().data("OK").build();
    }

    @PostMapping("/university-statistics/all-data/sign-up")
    public RestResult allSignUpData(@RequestParam(value = "activityId", required = false) String activityId,@RequestBody UniversityStatisticsQueryVO req) {
        universityStatisticsService.allSignUpData(activityId,req);
        return RestResultBuilder.builder().success().data("OK").build();
    }

}
